export const navigateTo = (url: string) => {
  (window as any).wx.miniProgram.navigateTo({ url });
};

export const config = (
  options: {
    appId: string;
    timestamp: string;
    nonceStr: string;
    signature: string;
    jsApiList: Array<"getLocation">;
  },
  success: () => void,
  fail?: (e: Error) => void
) => {
  const wx = (window as any).wx;
  wx.config({ ...options, debug: false });
  wx.ready(() => success());
  wx.error((error: any) => {
    console.error(error);
    fail?.(new Error(error.errMsg));
  });
};

export const getLocation = (
  type: "gcj02" | "wgs84",
  success: (e: { longitude: string; latitude: string }) => void,
  fail?: (e: Error) => void
) => {
  (window as any).wx.getLocation({
    type,
    success: success,
    fail: (error: any) => {
      fail?.(new Error(error.errMsg));
    },
  });
};
